//
//  NSString+SLSDK.m
//  SLTool
//
//  Created by SongLian on 16/7/10.
//  Copyright © 2016年 SongLian. All rights reserved.
//

#import "NSString+SLSDK.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (SLSDK)

- (NSString *)sl_md5String {
    
    const char *string = self.UTF8String;
    int length = (int)strlen(string);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(string, length, bytes);
    
    NSMutableString *mutableString = SLMutableString;
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [mutableString appendFormat:@"%02x", bytes[i]];
    
    return [NSString stringWithString:mutableString];
}

- (BOOL)sl_isValidMobilePhoneNumber {
    //手机号以13，15，18，17开头
    NSString *phoneRegex = @"^1(3[0-9]|5[0-35-9]|8[025-9]|70|77)\\d{8}$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:self];
}

- (BOOL)sl_isEmptyString {
    return [self isEqualToString:SLString_Empty];
}

- (BOOL)sl_isZeroString {
    return [self isEqualToString:SLString_Zero];
}

- (NSString *)sl_stringWithTimestamp {
    NSTimeInterval timestamp = [self doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}

+ (NSString *)sl_string_daysHoursMinutes_withTimeInterval:(NSInteger)timeInterval {
    NSInteger minutes = (timeInterval / 60) % 60;
    NSInteger hours = (timeInterval / 60 / 60) % 24;
    NSInteger days = timeInterval / 60 / 60 / 24;
    return [NSString stringWithFormat:@"%ld天%ld小时%ld分", days, hours, minutes];
}


- (NSString *)sl_substringWithLastCharacter {
    return [self substringToIndex:self.length - 1];
}

+ (NSString *)sl_stringWithURLString:(NSString *)urlString parameters:(NSDictionary *)parameters {
    
    NSMutableString *string = SLMutableString;
    [parameters enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        [string appendString:[NSString stringWithFormat:@"%@=%@&", key, obj]];
    }];
    
    string = [[string sl_substringWithLastCharacter] mutableCopy];
    NSString *signs = [NSString stringWithFormat:@"%@?%@", urlString, string];
    //    NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    //    signs = [signs stringByAddingPercentEscapesUsingEncoding:gbkEncoding];
    return signs;
}


- (NSString *)sl_transformToChinesePinYin {
    
    NSMutableString *mutabelString = [self mutableCopy];
    //  转换成拼音
    CFStringTransform((__bridge CFMutableStringRef)mutabelString, NULL, kCFStringTransformMandarinLatin, NO);
    //  去除声调
    CFStringTransform((__bridge CFMutableStringRef)mutabelString, NULL, kCFStringTransformStripDiacritics, NO);
    return [mutabelString copy];
}

@end
